package com.cuput.partybuilding.contact.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuput.partybuilding.contact.R;
import com.cuput.partybuilding.contact.bean.Contact;

import java.util.ArrayList;
import java.util.Random;

/**
 * Created by wei on 2017-05-09.
 * 联系人列表适配器
 */

public class ContactGroupItemAdapter extends RecyclerView.Adapter<ContactGroupItemAdapter.GroupItemVH> {
    private Context mContext;
    private ArrayList<Contact> mContacts;
    // 颜色资源数组
    private int[] colorsRes = {
            R.color.colorItem1,
            R.color.colorItem2,
            R.color.colorItem3,
            R.color.colorItem4,
            R.color.colorItem5,
            R.color.colorItem6
    };
    private Random random = new Random();

    public ContactGroupItemAdapter(Context context, ArrayList<Contact> contacts) {
        this.mContext = context;
        this.mContacts = contacts;
    }

    public void setContacts(ArrayList<Contact> mContacts) {
        this.mContacts = mContacts;
    }

    @Override
    public GroupItemVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_group, parent, false);
        GroupItemVH groupItemVH = new GroupItemVH(view);
        groupItemVH.setViewType(viewType);
        return groupItemVH;
    }

    @Override
    public void onBindViewHolder(GroupItemVH holder, int position) {
        Contact contact = mContacts.get(position);
        holder.portrait.setText(getPortraitStr(contact.getContractName()));
        GradientDrawable gradientDrawable = (GradientDrawable) holder.portrait.getBackground();
        int colorIndex = random.nextInt(colorsRes.length);
        gradientDrawable.setColor(mContext.getResources().getColor(colorsRes[colorIndex]));

        holder.contractName.setText(contact.getContractName());
        holder.phoneNumber.setText(contact.getPhoneNumber());
    }

    @Override
    public int getItemCount() {
        return mContacts == null ? 0 : mContacts.size();
    }

    static class GroupItemVH extends RecyclerView.ViewHolder {
        public TextView portrait;
        public TextView contractName;
        public TextView phoneNumber;
        private int viewType;

        public GroupItemVH(View view) {
            super(view);
            portrait = (TextView) view.findViewById(R.id.portrait);
            contractName = (TextView) view.findViewById(R.id.contactName);
            phoneNumber = (TextView) view.findViewById(R.id.phoneNumber);
        }

        public int getViewType() {
            return viewType;
        }

        public void setViewType(int viewType) {
            this.viewType = viewType;
        }
    }

    /**
     * 生成联系人头像的文字
     * 2个以上的字符截取最后两位
     * @param text
     * @return
     */
    private String getPortraitStr(String text) {
        String portraitStr = null;

        if (text == null) {
            return portraitStr;
        } else if (text.length() == 1) {
            portraitStr = text;
            return portraitStr;
        } else {
            portraitStr = text.substring(text.length() - 2);
            return portraitStr;
        }
    }
}
